package com.ssm.xm.controller.teacher;

import com.ssm.xm.model.ThInfo;
import com.ssm.xm.service.teacher.ThInfoService;
import com.ssm.xm.service.teacher.impl.ThInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/theacher")
public class TeacherController {
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));//true:允许为空, false:不允许为空
    }

    @Autowired
    private ThInfoService thInfoService;

    //公用业务
    //调用业务--查询教师信息
    ThInfo getTheacherMessage(int thId){
        return thInfoService.queryByTeacheId(thId);
    }

//    教师信息查询
    @RequestMapping("/message")
    String message(HttpServletRequest request, HttpServletResponse response, Model model,int thId){
        //获取数据--教师ID
        // 获取教师对象--调用getTheacherMessage方法
        ThInfo thInfo = getTheacherMessage(thId);
        //返回数据
        model.addAttribute("thInfo", thInfo);
        //跳转页面--showTeacherInfo.jsp
        return "/view/teacher/showTeacherInfo";
    }
//    //    教师信息修改
    @RequestMapping("/fix")// 跳转修改页面
    String fixUp(HttpServletRequest request, HttpServletResponse response, Model model,int thId){
        //获取数据--教师对象
        // 获取教师对象--调用getTheacherMessage方法
        ThInfo thInfo = getTheacherMessage(thId);
        model.addAttribute("thInfo", thInfo);
        //跳转页面--updateTeacherInfo.jsp
        return "/view/teacher/updateTeacherInfo";
    }
    @RequestMapping("/update")// 调用修改业务
    String update(HttpServletRequest request, HttpServletResponse response,ThInfo thInfo){
        //获取数据--教师对象
        // 调用业务
        thInfoService.updateThInfoBythId(thInfo);
        //跳转页面--重定向：查询教师信息页面
        return "redirect:/theacher/message?thId="+thInfo.getThId();
    }


    //    //    教师账户修改
    @RequestMapping("/fixAccount")// 跳转修改账户页面
    String fixAccount(HttpServletRequest request, HttpServletResponse response, Model model,int thId){
        //获取数据--教师账户信息
        // 获取教师对象--调用getTheacherMessage方法
        ThInfo thInfo = getTheacherMessage(thId);
        model.addAttribute("thInfo", thInfo);
        //跳转页面--updateTeacherInfo.jsp
        return "/view/teacher/updateTeacherAccountInfo";
    }
    @RequestMapping("/updateAccount")// 调用修改账户业务
    String updateAccount(HttpServletRequest request, HttpServletResponse response, Model model,ThInfo thInfo){
        //获取数据--教师对象
        System.out.println(thInfo);
        // 调用业务
        thInfoService.updateThInfoBythId(thInfo);
        //跳转页面--重定向：查询教师信息页面
        return "redirect:/theacher/message?thId="+thInfo.getThId();
    }


}
